package com.project.controller;

import com.project.dto.MaterialRequestDTO;
import com.project.dto.MaterialRequestPageDTO;
import com.project.result.Result;
import com.project.service.MeterialRequestsService;
import com.project.vo.Approvals_DetailVO;
import com.project.vo.MaterialRequestPageVO;
import com.project.vo.MaterialRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 材料申请管理功能
 */
@RestController
@Slf4j
@RequestMapping("/material-requests")
public class MeterialRequestsController {
    @Autowired
    private MeterialRequestsService meterialRequestsService;


    /**
     * 根据申请ID查询申请详细信息
     * @param id 申请id
     * @return 统一响应
     */
    @GetMapping("/{id}")
    public Result<MaterialRequestVO> getById(@PathVariable Integer id){
        log.info("根据申请ID查询申请详细信息：{}",id);
        MaterialRequestVO materialRequestVO=meterialRequestsService.getByRequestId(id);
        return Result.success(materialRequestVO);
    }


    /**
     * 获取待审批申请数量
     * @return 待审批数量
     */
    @GetMapping("/pending-count")
    public Result<Long> getPendingCount(){
        log.info("获取待审批申请数量");
        Long count= meterialRequestsService.getPendingCount();
        return Result.success(count);
    }


    /**
     * 查询材料申请列表分页
     * @return
     */
    @GetMapping
    public Result<MaterialRequestPageVO> list(MaterialRequestPageDTO materialRequestPageDTO){
        MaterialRequestPageVO materialRequestPageVO =meterialRequestsService.listPage(materialRequestPageDTO);
        return  Result.success(materialRequestPageVO);
    }

    /**
     * 根据审批实例申请表
     * @param approval_id
     * @return
     */
    @GetMapping("/approval/{approval_id}")
    public Result<Approvals_DetailVO> list(@PathVariable Integer approval_id){
        log.info("根据审批实例获取审批详情表：{}",approval_id);
        Approvals_DetailVO approvalsDetailVO=meterialRequestsService.getByApprovalsId(approval_id);
        return  Result.success(approvalsDetailVO);
    }


    /**
     * 创建申请表
     * @param materialRequestDTO
     * @return
     */
    @PostMapping
    public Result createRequest(@RequestBody MaterialRequestDTO materialRequestDTO){
        log.info("创建申请表：{}",materialRequestDTO);
        meterialRequestsService.createRequest(materialRequestDTO);
        return Result.success();
    }


}
